//
//  OMStarView.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/17.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMStarView.h"
@interface OMStarView()
@property (nonatomic, strong) NSMutableArray<UIButton*> *starBtns;

@end

@implementation OMStarView
- (instancetype)init
{
    self = [super init];
    [self configure];
    return self;
}

- (void)configure
{
    NSUInteger num = 5;
    _starBtns = [NSMutableArray array];
    for (NSUInteger i = 0; i < num; i ++) {
        UIButton *button = [UIButton new];
        UIButton *lastButton = nil;
        if (i != 0) {
            lastButton = _starBtns[i-1];
        }
        [button setImage:[UIImage imageNamed:@"ic_order_evaluate_hl"] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:@"ic_order_evaluate_nr"] forState:UIControlStateNormal];
        [button setContentEdgeInsets:UIEdgeInsetsMake(SM_SIZE(20), SM_SIZE(15), SM_SIZE(20), SM_SIZE(15))];
        [self addSubview:button];
        [button addTarget:self action:@selector(buttonTap:) forControlEvents:UIControlEventTouchUpInside];
        [_starBtns addObject:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(@0);
            if (i == 0) {
                make.left.equalTo(@0);
            }else {
                make.left.equalTo(lastButton.mas_right).offset(SM_SIZE(0));
            }
            if (i == num - 1) {
                make.right.equalTo(@0);
            }
        }];
    }
}

- (void)buttonTap:(UIButton *)btn
{
    NSUInteger index = [_starBtns indexOfObject:btn];
    for (NSInteger i = 0; i <= index; i ++) {
        UIButton *button = _starBtns[i];
        button.selected = true;
        [button addInstanceScaleAnimationWithTargetScale:1.5 reverse:YES duration:.15];
    }
    for (NSInteger i = index + 1; i < _starBtns.count; i ++) {
        UIButton *button = _starBtns[i];
        button.selected = false;
    }

    self.point = index + 1;
}

- (void)setPoint:(NSUInteger)point
{
    _point = point;
    NSInteger index = point - 1;
    for (NSInteger i = 0; i <= index; i ++) {
        UIButton *button = _starBtns[i];
        button.selected = true;
        [button addInstanceScaleAnimationWithTargetScale:1.5 reverse:YES duration:.15];
    }
}

@end
